package com.cqut.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cqut.dao.EvaluationEdMapper;
import com.cqut.dto.LimitShowDTO;
import com.cqut.model.EvaluationEd;
import com.cqut.util.SqlUtil;
import com.cqut.service.IEvaluationEdService;

@Service
public class EvaluationEdService implements IEvaluationEdService {
    @Resource(name="evaluationEdMapper")
    private EvaluationEdMapper evaluationEdMapper;

    @Override
    public boolean save(EvaluationEd evaluationEd) {
        return evaluationEdMapper.insert(evaluationEd) > 0;
    }

    @Override
    public boolean update(EvaluationEd evaluationEd) {
        return evaluationEdMapper.updateByPrimaryKeySelective(evaluationEd) >= 0;
    }

    @Override
    public boolean delete(String evaluationId) {
        return evaluationEdMapper.deleteByPrimaryKey(evaluationId) > 0;
    }

    @Override
    public boolean batchDelete(String[] primaryKeys) {
        return evaluationEdMapper.deleteByPrimaryKeys(primaryKeys) > 0;
    }

    @Override
    public EvaluationEd findModel(String evaluationId) {
        return evaluationEdMapper.selectByPrimaryKey(evaluationId);
    }

    @Override
    public EvaluationEd findModel(String evaluationId, String[] parameters) {
        return evaluationEdMapper.findModel(evaluationId, SqlUtil.formatParameters(parameters));
    }

    public Map<String, Object> findMap(String evaluationId) {
        String[][] parameters = {{"evaluation_id", "evaluationId"}, {"evaluation_name", "evaluationName"}, {"creater_id", "createrId"}, {"creater_name", "createrName"}, {"creater_time", "createrTime"}};
        return this.findMap(evaluationId, parameters);
    }

    public Map<String, Object> findMap(String evaluationId, String[][] parameters) {
        return evaluationEdMapper.findMap(evaluationId, SqlUtil.formatParametersForAlias(parameters));
    }

    @Override
    public List<EvaluationEd> loadModels() {
        return this.loadModels(null, null, null, null, -1, -1);
    }

    @Override
    public List<EvaluationEd> loadModels(String condition, Object[] values, String order, String sort, int curPage, int limit) {
        return this.loadModels(null, condition, values, order, sort, curPage, limit);
    }

    @Override
    public List<EvaluationEd> loadModels(String[] parameters, String condition, Object[] values, String order, String sort, int curPage, int limit) {
        return evaluationEdMapper.loadModels(SqlUtil.formatParameters(parameters), SqlUtil.fillCondition(condition, values), order, sort, SqlUtil.getOffset(curPage, limit), limit);
    }

    @Override
    public List<Map<String, Object>> loadMaps() {
        String[][] parameters = {{"evaluation_id", "evaluationId"}, {"evaluation_name", "evaluationName"}, {"creater_id", "createrId"}, {"creater_name", "createrName"}, {"creater_time", "createrTime"}};
        return this.loadMaps(parameters, null, null, null, null, -1, -1);
    }

    @Override
    public List<Map<String, Object>> loadMaps(String condition, Object[] values, String order, String sort, int curPage, int limit) {
        String[][] parameters = {{"evaluation_id", "evaluationId"}, {"evaluation_name", "evaluationName"}, {"creater_id", "createrId"}, {"creater_name", "createrName"}, {"creater_time", "createrTime"}};
        return this.loadMaps(parameters, condition, values, order, sort, curPage, limit);
    }

    @Override
    public List<Map<String, Object>> loadMaps(String[][] parameters, String condition, Object[] values, String order, String sort, int curPage, int limit) {
        return evaluationEdMapper.loadMaps(SqlUtil.formatParametersForAlias(parameters), SqlUtil.fillCondition(condition, values), order, sort, SqlUtil.getOffset(curPage, limit), limit);
    }

    @Override
    public long count() {
        return this.count(null, false);
    }

    @Override
    public long count(String condition, Object[] values) {
        return this.count(null, condition, values, false);
    }

    @Override
    public long count(String[] parameters, boolean isDistinct) {
        return this.count(parameters, null, null, isDistinct);
    }

    @Override
    public long count(String[] parameters, String condition, Object[] values, boolean isDistinct) {
        return evaluationEdMapper.count(SqlUtil.formatParameters(parameters), SqlUtil.fillCondition(condition, values), isDistinct);
    }
    
    /**
     * 获得list
     * **/
    public List<Map<String, Object>> getEvaluationEdList(String editionName,LimitShowDTO limitShowDTO){
    	return evaluationEdMapper.getListByEditionName(editionName,limitShowDTO.calculateStart(),limitShowDTO.calculateEnd());
    }
    
    /**
     * 获得count
     * **/
    public long getEvaluationEdCount(String editionName){
    	return evaluationEdMapper.getCountByEditionName(editionName);
    }
    
    /**
     * 新增版本
     * **/
    public boolean addNewVersion(EvaluationEd evaluationEd){
    	evaluationEd.setEvaluationId(getNewEvaluationId());
    	String userName = evaluationEdMapper.getNameByUserId(evaluationEd.getCreaterId());
    	evaluationEd.setCreaterName(userName);

    	return evaluationEdMapper.addNewVersion(evaluationEd)> 0;
    }
    
    /**
     * 获得版本新id
     * **/
    public String getNewEvaluationId(){
    	Map<String, Object> maxId = evaluationEdMapper.getMaxEvaluationId();
    	
		String temp = maxId.values().toString();
		
		int max = Integer.parseInt(temp.substring(1,temp.length()-1));
		max++;
		String maxNum = Integer.toString(max);
		switch(maxNum.length()){
		case 1: return "000"+max;
		case 2: return "00"+max;	
		case 3: return "0"+max;
		}
		return "";
    }

	@Override
	public Map<String, Object> getEvaluationById(String evaluationId) {
		// TODO Auto-generated method stub
		return evaluationEdMapper.getEvaluationById(evaluationId);
	}

	@Override
	public boolean updateEvaluation(EvaluationEd evaluationEdModel) {
		// TODO Auto-generated method stub
		return evaluationEdMapper.updateByPrimaryKeySelective(evaluationEdModel)>=0;
	} 
}
